
package com.yunfei.api.user;
import com.yunfei.api.user.contants.UserCenterContants;
import com.yunfei.api.user.param.AccountLoginForAdminParam;
import com.yunfei.api.user.param.UpdateUserInfoParam;
import com.yunfei.api.user.responseBody.LoginAndRegiterResult;
import com.yunfei.api.user.responseBody.LoginOutResult;
import com.yunfei.api.user.result.*;
import com.yunfei.api.user.valid.LoginForm;
import com.yunfei.api.user.valid.MobileForm;
import com.yunfei.api.user.valid.TokenForm;
import com.yunfei.bo.TPersonalUserBo;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.contants.ApplicationConstant;
import com.yunfei.contants.CodeMessageConstant;
import com.yunfei.contants.RedisConstants;
import com.yunfei.contants.TokenResult;
import com.yunfei.entity.TPersonalUserEntity;
import com.yunfei.enums.ResultStatus;
import com.yunfei.fegin.RedisFeignService;
import com.yunfei.mapper.aiiidb.GetStatusMapper;
import com.yunfei.mapper.aiiidb.TCrmUserMapper;
import com.yunfei.mapper.aiiidb.TPersonalUserMapper;
import com.yunfei.ribbon.RedisServiceRibbon;
import com.yunfei.service.RedisService;
import com.yunfei.service.ValidMobileAndCodeService;
import com.yunfei.utils.*;
import com.yunfei.utils.id.IdUtils;
import com.yunfei.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 用户中心
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@Api(value = UserCenterContants.USER_CENTER_API_NAMESPACE,tags = UserCenterContants.USER_CENTER_API_NAMESPACE )
@RestController
@RequestMapping("/api/v2/user")
public class UserCenterApiController {
    @Autowired
    private TPersonalUserMapper tPersonalUserMapper;
    @Autowired
    private GetStatusMapper getStatusMapper;
    @Autowired
    private RedisFeignService redisFeignService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RedisServiceRibbon redisServiceRibbon;
    @Autowired
    private ValidMobileAndCodeService validMobileAndCodeService;
    @Autowired
    private TCrmUserMapper tCrmUserMapper;
    @Autowired
    private Environment env;
    @RequestMapping(value = "/getShuangLuUserInfo",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ApiOperation(value = UserCenterContants.API_GET_TOKEN_INFO,notes =  UserCenterContants.AUTHOR_NOTE,position = 3,response = UserInfoResult.class)
    @ResponseBody
    public ResultModel postShuangLuUserInfo(TokenForm tokenRequest){
        ResultModel resultModel=new ResultModel();
        try{
            String token=tokenRequest.getToken();//token原
                //参数校验成功
                //校验验证码是否正确
                System.out.println("token=================="+token);
                if(token.contains("%3D")){
                    token=token.replace("%3D","=");
                }
                System.out.println("转换后token=================="+token);
                String key=token;
                boolean flag=redisService.exists(key);
                if(flag){
                    //token有效
                    String userId=TokenUtil.getInstance().getUserId(token);
                    ShuangLuUserInfoResult userInfoResult=new ShuangLuUserInfoResult();
                    userInfoResult.setCustNo(userId);
                    TPersonalUserEntity tp=tPersonalUserMapper.findById(userId);
                    if(!StringUtil.isNullOrEmpty(tp)){
                        String cCustNo=tPersonalUserMapper.getCustNo(tp.getMobile());
                        if(!StringUtil.isNullOrEmpty(cCustNo)){
                            userInfoResult.setCustNo(cCustNo);
                        }
                        userInfoResult.setNickName(tp.getUserName());//姓名
                        userInfoResult.setMobile(tp.getMobile());//手机号
                        userInfoResult.setIdentity(tp.getUserType());//用户类型
                        userInfoResult.setHeadIcon(tp.getIconUrl());//头像
                        userInfoResult.setRemark(tp.getRemark());
                        String userType=tp.getUserType();
                        if(UserCenterContants.USER_TYPE_01.equals(userType)){
                            //查询其客户经理对应的客户号
                            String liCaishiCustNo= tPersonalUserMapper.getLiCaiShiCustNo(cCustNo);
                            if(!StringUtil.isNullOrEmpty(liCaishiCustNo)){
                                userInfoResult.setLiCaiShiCustNo(liCaishiCustNo);
                            }
                        }
                        resultModel.setData(userInfoResult);
                        resultModel.setCode(ResultStatus.SUCCESS.getCode());
                        resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
                    }

                }else{
                    resultModel.setCode(ResultStatus.TOKEN_ERROR.getCode());
                    resultModel.setMessage(ResultStatus.TOKEN_ERROR.getMessage());
                    return resultModel;
                }
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }

        return resultModel;
    }

    @RequestMapping(value = "/getShuangLuUserInfo",method = RequestMethod.GET)
    @ApiOperation(value = UserCenterContants.API_GET_TOKEN_INFO,notes =  UserCenterContants.AUTHOR_NOTE,position = 3,response = UserInfoResult.class)
    @ResponseBody
    public ResultModel getShuangLuUserInfo(TokenForm tokenRequest){
        ResultModel resultModel=new ResultModel();
        try{
            String token=tokenRequest.getToken();//token原
                //参数校验成功
                //校验验证码是否正确
                System.out.println("token=================="+token);
                if(token.contains("%3D")){
                    token=token.replace("%3D","=");
                }
                System.out.println("转换后token=================="+token);
                String key=token;
                boolean flag=redisService.exists(key);
                if(flag){
                    //token有效
                    String userId=TokenUtil.getInstance().getUserId(token);
                    ShuangLuUserInfoResult userInfoResult=new ShuangLuUserInfoResult();
                    userInfoResult.setCustNo(userId);
                    TPersonalUserEntity tp=tPersonalUserMapper.findById(userId);
                    if(!StringUtil.isNullOrEmpty(tp)){
                        String cCustNo=tPersonalUserMapper.getCustNo(tp.getMobile());
                        if(!StringUtil.isNullOrEmpty(cCustNo)){
                            userInfoResult.setCustNo(cCustNo);
                        }
                        userInfoResult.setNickName(tp.getUserName());//姓名
                        userInfoResult.setMobile(tp.getMobile());//手机号
                        userInfoResult.setIdentity(tp.getUserType());//用户类型
                        userInfoResult.setHeadIcon(tp.getIconUrl());//头像
                        userInfoResult.setRemark(tp.getRemark());
                        String userType=tp.getUserType();
                        if(UserCenterContants.USER_TYPE_01.equals(userType)){
                            //查询其客户经理对应的客户号
                            String liCaishiCustNo= tPersonalUserMapper.getLiCaiShiCustNo(cCustNo);
                            if(!StringUtil.isNullOrEmpty(liCaishiCustNo)){
                                userInfoResult.setLiCaiShiCustNo(liCaishiCustNo);
                            }
                        }
                        resultModel.setData(userInfoResult);
                        resultModel.setCode(ResultStatus.SUCCESS.getCode());
                        resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
                    }

                }else{
                    resultModel.setCode(ResultStatus.TOKEN_ERROR.getCode());
                    resultModel.setMessage(ResultStatus.TOKEN_ERROR.getMessage());
                    return resultModel;
                }
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }

        return resultModel;
    }

}
